﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Weibo4Sharp.Model;
using Weibo4Sharp.Net;
using System.Web;
using Weibo4Sharp.Serialization;

namespace Weibo4Sharp.Service
{
    public partial class WeiboService
    {
        private HttpPost PrepareHttpPostRequest()
        {
            var httpRequest = new HttpPost();

            httpRequest.AppKey = _consumerKey;
            httpRequest.AppSecret = _consumerSecret;
            httpRequest.Token = _token;
            httpRequest.TokenSecret = _tokenSecret;

            return httpRequest;
        }

        public virtual WeiboStatus SendWeiboWithPic(string status, string filename)
        {
            var httpRequest = PrepareHttpPostRequest();

            var response = httpRequest.RequestWithPicture(Globals.RestAPIAuthority + "statuses/upload" + FormatAsString, "?status=" + HttpUtility.UrlEncode(status), filename);

            return GetEntity<WeiboStatus>(response);
        }

        public virtual WeiboStatus SendWeiboWithPic(string status, string filename, double lat, double @long)
        {
            var httpRequest = PrepareHttpPostRequest();

            var response = httpRequest.RequestWithPicture(
                Globals.RestAPIAuthority + "statuses/upload" + FormatAsString,
                "?status=" + HttpUtility.UrlEncode(status) +
                "&lat=" + lat +
                "&long=" + @long
                , filename);

            return GetEntity<WeiboStatus>(response);
        }

        private T GetEntity<T>(string content)
        {
            var serializer = new JsonSerializer();
            var entity = serializer.Deserialize<T>(content);

            return entity;
        }

        //public virtual WeiboStatus SendWeibo(string status, string filename, double lat, double @long)
        //{
        //    return WithHammockPic<WeiboStatus>("statuses/upload", filename, FormatAsString, "?status=", status, "&lat=", lat, "&long=", @long);
        //}
    }
}
